<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{ path: '/' }">
      <i class="el-icon-s-promotion"></i>后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/house/section' }">区域管理</el-breadcrumb-item>
    <el-breadcrumb-item><a href="/">添加区域</a></el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <div>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <div style="width: 30%;">
    <el-form-item label="区域名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    </div>
    <el-form-item label="父级类别" prop="parentId">
      <el-cascader
          style="width: 250px;"
          placeholder="请选择父级类别，如不选择，则表示无父级类别"
          :props="{ checkStrictly: true }"
          v-model="ruleForm.parentId"
          :options="parentSectionOptions"></el-cascader>
      <span>如果父级类别列表不准确，请点击 <a class="simple-link" href="#" @click="loadSectionTree">这里</a> 刷新。</span>
    </el-form-item>
    <div style="width: 20%;">
    <el-form-item label="层级" prop="depth">
      <el-input v-model="ruleForm.depth"></el-input>
    </el-form-item>
    </div>
    <el-form-item label="是否为父级" prop="isParent">
      <el-switch
          v-model="ruleForm.isParent"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ccc">
      </el-switch>
    </el-form-item>
    <el-form-item label="是否显示" prop="isDisplay">
      <el-switch
          v-model="ruleForm.isDisplay"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ccc">
      </el-switch>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
      <el-button type="warning" @click="$router.push('/house/section')">返回列表</el-button>
    </el-form-item>
  </el-form>
  </div>
</div>
</template>

<script>
export default {
  data() {
    return {
      // 父级类别的下拉菜单的选项数据
      parentSectionOptions: [],
      ruleForm: {
        name: '测试数据001',
        parentId:'',
        depth:"1",
        isParent:'',
        isDisplay:'',
      },
      rules: {
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        parentId: [
          {required: true, message: '请输入父级类别ID', trigger: 'blur'},
          // {pattern: /^\d+$/, message: '请选择有效的父级类别', trigger: 'blur'}
        ],
        depth: [
          {required: true, message: '请输入层级', trigger: 'blur'},
          {pattern: /^[1-9]\d*$/, message: '请选择有效的层级', trigger: 'blur'}
        ],
        isParent: [
          {required: true, message: '请选择是否为父级', trigger: 'blur'},
        ],
        isDisplay: [
          {required: true, message: '请选择是否显示', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9200/section/add-new';
          console.log('url = ' + url);

          this.ruleForm.parentId = this.ruleForm.parentId.pop();
          console.log('ruleform', this.ruleForm);

          // let  formData = 'name=' + this.ruleForm.name
          //       + '&description=' + this.ruleForm.description
          //       + '&sort=' + this.ruleForm.sort;

          let formData = this.qs.stringify(this.ruleForm);

          console.log("formData=" + formData)
          this.axios.post(url,formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state==20000){
              this.$message({
                message: '添加区域成功',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 加载类别树的数据
    loadSectionTree() {
      let url = 'http://localhost:9200/section/tree';
      console.log('url = ' + url);


      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000) {
          this.parentSectionOptions = jsonResult.data;
          this.parentSectionOptions.unshift({
            value: 0,
            label: '无父级类别'
          });
          this.ruleForm.parentId = "0";
        }
      });
    }
  },
  mounted(){
    this.loadSectionTree();
  }
}
</script>

<style scoped>

</style>